package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

@Controller
public class ReturnController {
    @RequestMapping("/index")
    public Object index(){
        return "/index.html";
    }

    @ResponseBody
    @RequestMapping("/returnData")
    public String returnData(){
        return "返回视图数据~";
    }

    @ResponseBody
    @RequestMapping("/returnHtml")
    public String returnHtml(){
        return "<h1>你好</h1>";
    }

    @ResponseBody
    @RequestMapping("/returnJson")
    public Person returnJson(){
        Person person = new Person();
        person.setId(1);
        person.setName("zhangsan");
        person.setWeight(50.5);
        return person;
    }

    @ResponseBody
    @RequestMapping("/returnMap")
    public Map<String, String> returnMap(){
        Map<String, String> map = new HashMap<>();
        map.put("username", "zhangsan");
        map.put("school", "giit");
        return map;
    }

    @ResponseBody
    @RequestMapping("/setStatus")
    public String setStatus(HttpServletResponse response){
        response.setStatus(404);
        return "设置状态码~";
    }

    @ResponseBody
    @RequestMapping(value = "/test", produces = "application/json;charset=utf8", consumes = "application/json;charset=utf8")
    //produces规定响应的格式,consumes规定请求的格式
    public String test(HttpServletResponse response){
        response.setHeader("xixi", "hh");
        return "ok";
    }
}
